package com.lynn.salary.web.action;

import java.util.Date;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

import com.lynn.salary.entity.Accessory;
import com.lynn.salary.entity.SystemFile;
import com.lynn.salary.pojo.FeedBack;
import com.lynn.salary.service.IAccessoryService;
import com.lynn.salary.service.ISystemFileService;
import com.lynn.salary.util.ObjectFactory;

public class AccessoryAction extends BaseAction {

	private IAccessoryService accessoryService = (IAccessoryService) ObjectFactory.getBean("accessoryService");
	private ISystemFileService systemFileService = (ISystemFileService) ObjectFactory.getBean("systemFileService");
	private Accessory accessory;
	private SystemFile systemFile;
	private FeedBack feedBack;
	private String[] ids;
	private String flag;
	private List<Map<String,Object>> sfs;
	
	private List<Map<String, Object>> accessoryList;
	public int perpage = 2;
	public int currentpage = 1;
	public long accessoryLength;

	public String showAccessory() throws Exception
	{
		accessoryList = accessoryService.showPartAsseroy(currentPage, perpage);
		 
		long length = accessoryService.findAllAccessoryNumber();
		if(length%perpage != 0)
		{
			accessoryLength = length/perpage + 1;
		}
		else
		{
			accessoryLength = length/perpage;
		}
		return "success";
	}
	
	public String getPerAccessory() throws Exception
	{
		accessoryList = accessoryService.showPartAsseroy(currentPage, perpage);
		return "getPerSuccess";
	}

	public String delete() throws Exception {

		boolean flag;
		try {
			flag = accessoryService.delete(ids, new Accessory());
		} catch (Exception e) {
			e.printStackTrace();
			flag = false;
		}

		feedBack = new FeedBack();
		feedBack.setStatusCode("200");
		if (flag) {
			feedBack.setMessage("删除成功");
		} else {
			feedBack.setMessage("删除失败");
		}
		feedBack.setRel("");
		feedBack.setNavTabId("allAccessory");
		feedBack.setForwardUrl("");
		feedBack.setCallbackType("");

		return "feedback";
	}

	public String edit() throws Exception {
		if (flag.equals("showEditPage")) {
			
			accessory = accessoryService.getByPk(accessory.getId(), Accessory.class);
			
			return "showEditPage";
		} else if (flag.equals("update")) {
				Map<String,Object> data = new HashMap<String,Object>();
				data.put("id", accessory.getId());
				data.put("name", accessory.getName());
				data.put("description", accessory.getDescription());
				data.put("alterTime", new Date());
				accessoryService.update(data, Accessory.class);

				feedBack = new FeedBack();
				feedBack.setStatusCode("200");
				feedBack.setMessage("更新成功");
				feedBack.setRel("updateAcceDialog");
				feedBack.setNavTabId("allAccessory");
				feedBack.setForwardUrl("");
				feedBack.setCallbackType("closeCurrent");
				return "feedback";
		}
		return "error";
	}
	
	public String create() throws Exception {
		if (flag.equals("showCreatePage")) {
			
			accessory = new Accessory();
			accessory.setCreator(loginUser.getLoginUser().getId());
			accessory.setName("新建附件");
			Date e = new Date();
			accessory.setNewTime(e);
			accessory.setAlterTime(e);
			accessory = accessoryService.save(accessory);
			
			return "showEditPage";
		} else if (flag.equals("update")) {
				Map<String,Object> data = new HashMap<String,Object>();
				data.put("id", accessory.getId());
				data.put("name", accessory.getName());
				data.put("description", accessory.getDescription());
				data.put("alterTime", new Date());
				accessoryService.update(data, Accessory.class);

				feedBack = new FeedBack();
				feedBack.setStatusCode("200");
				feedBack.setMessage("更新成功");
				feedBack.setRel("updateAcceDialog");
				feedBack.setNavTabId("allAccessory");
				feedBack.setForwardUrl("");
				feedBack.setCallbackType("closeCurrent");
				return "feedback";
		}
		return "error";
	}
	
	public String search() throws Exception {
		
		return "search";
	}

	public String addFile() throws Exception {
		if(flag.equals("showAddFile")) {
			accessory = accessoryService.getByPk(accessory.getId(), Accessory.class);
			if(accessory == null) {
				return "404";
			}
			return "showAddFile";
		}
		
		return "";
	}
	
	public String showFile() throws Exception {
		String[]part = new String[] {"id" , "fileName"};
		sfs = systemFileService.getPartByCondition(SystemFile.class, part, "tag like '%"+accessory.getId()+"%'");
		
		return "showFile";
	}
	
	public String rmFile() throws Exception {
		String aid = accessory.getId();
		String fid = systemFile.getId();
		
		systemFileService.removeTag(new String[]{fid}, aid+";", systemFile);
		
		return showFile();
	}
	
	public String publish() throws Exception {
		boolean flag;
		try {
			flag = accessoryService.removeTag(ids, Accessory.DRAFT, new Accessory());
			if (flag)
				flag = accessoryService.addTag(ids, Accessory.PUBLISH, new Accessory());
		} catch (Exception e) {
			e.printStackTrace();
			flag = false;
		}

		feedBack = new FeedBack();
		feedBack.setStatusCode("200");
		if (flag) {
			feedBack.setMessage("发布成功");
		} else {
			feedBack.setMessage("发布失败");
		}
		feedBack.setRel("");
		feedBack.setNavTabId("allAccessory");
		feedBack.setForwardUrl("");
		feedBack.setCallbackType("");

		return "feedback";
	}

	public String undoPublish() throws Exception {
		boolean flag;
		try {
			flag = accessoryService.removeTag(ids, Accessory.PUBLISH, new Accessory());
			if (flag)
				flag = accessoryService.addTag(ids, Accessory.DRAFT, new Accessory());
		} catch (Exception e) {
			e.printStackTrace();
			flag = false;
		}

		feedBack = new FeedBack();
		feedBack.setStatusCode("200");
		if (flag) {
			feedBack.setMessage("撤销成功");
		} else {
			feedBack.setMessage("撤销失败");
		}
		feedBack.setRel("");
		feedBack.setNavTabId("allAccessory");
		feedBack.setForwardUrl("");
		feedBack.setCallbackType("");

		return "feedback";
	}
	
	public IAccessoryService getAccessoryService() {
		return accessoryService;
	}

	public void setAccessoryService(IAccessoryService accessoryService) {
		this.accessoryService = accessoryService;
	}

	public Accessory getAccessory() {
		return accessory;
	}

	public void setAccessory(Accessory accessory) {
		this.accessory = accessory;
	}

	public String[] getIds() {
		return ids;
	}

	public void setIds(String[] ids) {
		this.ids = ids;
	}

	public FeedBack getFeedBack() {
		return feedBack;
	}

	public void setFeedBack(FeedBack feedBack) {
		this.feedBack = feedBack;
	}

	public String getFlag() {
		return flag;
	}

	public void setFlag(String flag) {
		this.flag = flag;
	}

	public ISystemFileService getSystemFileService() {
		return systemFileService;
	}

	public void setSystemFileService(ISystemFileService systemFileService) {
		this.systemFileService = systemFileService;
	}

	public List<Map<String, Object>> getSfs() {
		return sfs;
	}

	public void setSfs(List<Map<String, Object>> sfs) {
		this.sfs = sfs;
	}

	public SystemFile getSystemFile() {
		return systemFile;
	}

	public void setSystemFile(SystemFile systemFile) {
		this.systemFile = systemFile;
	}

	public List<Map<String, Object>> getAccessoryList() {
		return accessoryList;
	}

	public void setAccessoryList(List<Map<String, Object>> accessoryList) {
		this.accessoryList = accessoryList;
	}

	public int getPerpage() {
		return perpage;
	}

	public void setPerpage(int perpage) {
		this.perpage = perpage;
	}

	public int getCurrentpage() {
		return currentpage;
	}

	public void setCurrentpage(int currentpage) {
		this.currentpage = currentpage;
	}

	public long getAccessoryLength() {
		return accessoryLength;
	}

	public void setAccessoryLength(long accessoryLength) {
		this.accessoryLength = accessoryLength;
	}
	

}
